package corej.exam.com.usermgmt;

/*
 * ��������FileAnalyzer
 * ��ȡ�ļ�/etc/passwd��/etc/group������
 * ʹ��DataBuilder�����ļ�������ȫ��ӳ���ڴ��С�
 */

import java.io.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class FileAnalyzer {
    private File file = null;

    private Set<User> userSet = null;

    private Set<Group> groupSet = null;

    private DataBuilder dataBuilder = null;

    public FileAnalyzer() {
        super();
    }

    public FileAnalyzer(String str) {
        this.file = new File(str);
        userSet = new HashSet<User>();
        groupSet = new HashSet<Group>();
        dataBuilder = new DataBuilder();
    }

    public FileAnalyzer(File file) {
        this.file = file;
        userSet = new HashSet<User>();
        groupSet = new HashSet<Group>();
        dataBuilder = new DataBuilder();
    }

    public Collection readUser() throws Exception {
        InputStream is = null;
        is = new FileInputStream(file + "\\User.dat");
        Reader isr = new InputStreamReader(is);
        BufferedReader in = new BufferedReader(isr);

        String str = null;
        while ((str = in.readLine()) != null) {
            userSet.add(dataBuilder.parseUser(str));
        }
        /*
           * MISTAKE: ��}�� while(in.readLine()!=null){
           * dataBuilder.parseUser(in.readLine()); }
           */
        in.close();
        return userSet;
    }

    public Collection readGroup() throws Exception {
        InputStream is = null;
        is = new FileInputStream(file + "\\Group.dat");
        Reader isr = new InputStreamReader(is);
        BufferedReader in = new BufferedReader(isr);

        String str = null;
        while ((str = in.readLine()) != null) {
            groupSet.add(dataBuilder.parseGroup(str));
        }
        in.close();
        return groupSet;
    }
}
